package com.helloandroid.ads


import android.widget.FrameLayout
import com.anythink.core.api.ATAdConst
import com.anythink.core.api.ATAdInfo
import com.anythink.core.api.AdError
import com.anythink.splashad.api.ATSplashAd
import com.anythink.splashad.api.ATSplashAdListener
import com.business.Analyse
import com.business.AnalyseKey
import com.business.TDKeys
import com.helloandroid.R
import com.helloandroid.base.BaseActivity
import com.helloandroid.tools.MyLog


class StartAdActivity : BaseActivity(), ATSplashAdListener {
    var splashAd : ATSplashAd? = null

    lateinit var container:FrameLayout

    var hasHandleJump = false

//    private fun gotoMainActivity() {
//        val intent = Intent(this, MainActivity::class.java)
//        intent.putExtra(IntentKey.ComeFromWhere, comefrom)
//        MyLog.elog("========WelcomeActivity comefrom:${comefrom}==================")
//        startActivity(intent)
//        finish()
//    }

    override fun getContentViewResId() = R.layout.splash_ad_show

    override fun initView() {
        container = findViewById(R.id.splash_ad_container)
        initSplashAd()
    }

    private fun initSplashAd(){
        // init ui
        val layoutParams = container.layoutParams
        layoutParams.width = resources.displayMetrics.widthPixels;
        layoutParams.height = (resources.displayMetrics.heightPixels * 0.85f).toInt();

        //v5.6.2新增，针对首次安装获取TopOn策略导致开屏广告加载超时，可传递各广告平台参数，直接发起请求
        //atMediationRequestInfo传入后，只针对首次请求开屏的情况生效，请详细阅读上方说明
//        val atMediationRequestInfo = TTATRequestInfo("5148247","887443685", false);
//        atMediationRequestInfo.adSourceId = "355830";
//        splashAd = ATSplashAd(this, container, TopOnPlacementId, atMediationRequestInfo, this)

//       splashAd = ATSplashAd(this, container, TDKeys.TopOnSplashPlacementId, this);
        splashAd = ATSplashAd(this,TDKeys.TopOnSplashPlacementId,this)

        //打印出此广告位下的各个广告平台广告源的相关参数（必须已经初始化过SDK且调用ATSDK.setNetworkLogDebug(true);后才会打印）
        // ATSplashAd.checkSplashDefaultConfigList(this, TopOnPlacementId, null);

        val localMap: MutableMap<String, Any> = HashMap()
        localMap[ATAdConst.KEY.AD_WIDTH] = layoutParams.width
        localMap[ATAdConst.KEY.AD_HEIGHT] = layoutParams.height

        //
        splashAd!!.setLocalExtra(localMap)
        if (splashAd!!.isAdReady){
            MyLog.elog("SplashAd is ready to show.")
            splashAd!!.show(this,container)
        }else{
            MyLog.elog( "SplashAd isn't ready to show, start to request.")
            splashAd!!.loadAd()
        }
        MyLog.info("===========StartAdActivity onCreate called!")
    }

    override fun initData() {
    }
    private fun jumpToMainActivity(){
        if (!hasHandleJump){
            hasHandleJump = true
        }
        checkJump()
    }

    override fun onDestroy() {
        super.onDestroy()
        splashAd?.let {
            it.onDestory()
        }
    }

    private fun checkJump(){
        if (hasHandleJump){
            finish()
        }
    }

    //--------------------------------------------------
    // 开屏广告回调
    override fun onAdLoaded() {
        MyLog.elog("开屏广告加载成功!")
        splashAd?.show(this,container)
        Analyse.report(AnalyseKey.Ad_ResumeSplashAd)
    }

    override fun onNoAdError(adError: AdError?) {
        //注意：禁止在此回调中执行广告的加载方法进行重试，否则会引起很多无用请求且可能会导致应用卡顿
        //AdError，请参考 https://docs.toponad.com/#/zh-cn/android/android_doc/android_test?id=aderror
        MyLog.info("=====onNoAdError: ${adError?.fullErrorInfo}");
        jumpToMainActivity();
    }

    override fun onAdShow(p0: ATAdInfo?) {
    }

    override fun onAdClick(p0: ATAdInfo?) {
    }

    override fun onAdDismiss(p0: ATAdInfo?) {
        MyLog.info("=====onAdDismiss: ${p0}");
        jumpToMainActivity()
    }

    override fun isUseFullScreenMode(): Boolean  = false
}

